package com.goodedeeds.domain.resource;

import com.goodedeeds.domain.IVendor;
import com.goodedeeds.domain.IVendorCategory;
import com.goodedeeds.domain.impl.db.VendorDao;
import com.youtills.resource.BaseResource;
import com.youtills.resource.CommonAttribute;
import com.youtills.resource.ResourceAttributeValue;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: rchiluve
 * Date: 2/23/14
 * Time: 7:48 PM
 * To change this template use File | Settings | File Templates.
 */
public class Vendor extends BaseResource implements IVendor {

    private final ResourceAttributeValue<String> vendorKey;
    private final ResourceAttributeValue<String> vendorName;
    private final ResourceAttributeValue<String> vendorDescription;
    private final ResourceAttributeValue<Integer> vendorStatus;
    private List<IVendorCategory> vendorCategories;
    private final ResourceAttributeValue<Integer> defaultPageId;
    private final ResourceAttributeValue<String> previewHtml;

    public Vendor() {
        //TODO - set appropriate resource type.
//        setResourceType(0);
        this.vendorKey = new ResourceAttributeValue<String>(this, CommonAttribute.key);
        this.vendorName = new ResourceAttributeValue<String>(this, CommonAttribute.name);
        this.vendorDescription = new ResourceAttributeValue<String>(this, CommonAttribute.description);
        this.vendorStatus = new ResourceAttributeValue<Integer>(this, CommonAttribute.status_type);
        this.defaultPageId = new ResourceAttributeValue<>(this, GoodeDeedsAttribute.defaultPageId);
        this.previewHtml = new ResourceAttributeValue<>(this, GoodeDeedsAttribute.preview_html);
    }

    public Long getVendorId() {
        return getResourceId();
    }

    public String getVendorName() {
        return vendorName.getValue();
    }

    public void setVendorName(String schoolName) {
        this.vendorName.setValue(schoolName);
    }

    public String getVendorDescription() {
        return vendorDescription.getValue();
    }

    public void setVendorDescription(String schoolDescription) {
        this.vendorDescription.setValue(schoolDescription);
    }

    public Integer getVendorStatus() {
        return vendorStatus.getValue();
    }

    public void setVendorStatus(Integer vendorStatus) {
        this.vendorStatus.setValue(vendorStatus);
    }

    public String getVendorKey() {
        return vendorKey.getValue();
    }

    public void setVendorKey(String schoolKey) {
        this.vendorKey.setValue(schoolKey);
    }

    public List<IVendorCategory> getVendorCategories() {
        if(vendorCategories == null){
            vendorCategories = VendorDao.getInstance().getVendorCategories(null, getVendorId());
        }
        return vendorCategories;
    }

    private List<Long> vendorCategoryIds;
    public List<Long> getVendorCategoryIds(){
        if(vendorCategoryIds == null && getVendorCategories() != null){
            vendorCategoryIds = new ArrayList<Long>(getVendorCategories().size());
            for(IVendorCategory category : getVendorCategories()){
                vendorCategoryIds.add(category.getCategoryId());
            }
        }
        return vendorCategoryIds;
    }

    public void setVendorCategoryIds(List<Long> vendorCategoryIds) {
        this.vendorCategoryIds = vendorCategoryIds;
    }

    public Integer getDefaultPageId() {
        return defaultPageId.getValue();
    }

    public void setDefaultPageId(Integer defaultPageId) {
        this.defaultPageId.setValue(defaultPageId);
    }

    public String getPreviewHtml() {
        return previewHtml.getValue();
    }

    public void setPreviewHtml(String previewHtml) {
        this.previewHtml.setValue(previewHtml);
    }

    @Override
    protected String getTableName() {
        return "ged__vendor";
    }

    @Override
    protected String getResourceIdColumn() {
        return "vendor_id";
    }
}
